package com.wj.wjpicturebackend.manager.rabbitmq;

import com.wj.wjpicture.infrastructure.config.RabbitMqConfig;
import com.wj.wjpicture.interfaces.dto.picture.PictureTaskMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class PictureTaskProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送图片处理任务消息
     * @param message 任务消息
     */
    public void sendPictureTask(PictureTaskMessage message) {
        try {
            // 发送缩略图生成任务
            rabbitTemplate.convertAndSend(
                    RabbitMqConfig.PICTURE_EXCHANGE,
                    RabbitMqConfig.THUMBNAIL_ROUTING_KEY,
                    message
            );

            // 发送格式转换任务（转为webp）
            rabbitTemplate.convertAndSend(
                    RabbitMqConfig.PICTURE_EXCHANGE,
                    RabbitMqConfig.FORMAT_CONVERT_ROUTING_KEY,
                    message
            );

            log.info("图片处理任务消息发送成功，pictureId: {}", message.getPictureId());
        } catch (Exception e) {
            log.error("图片处理任务消息发送失败，pictureId: {}", message.getPictureId(), e);
            // 可选：发送失败时记录到数据库，后续人工补偿
        }
    }
}